#!/bin/bash

##
## This script builds and fires up a lobby and a bot on localhost.
## It then executes basic start-up checks and then executes a java
## smoke-testing client to verify that a java socket connection can 
## established to both lobby and bot. Finally this script checks
## logs to verify there were no errors (example, if bot fails to connect to lobby
## it will keep running but will log an error).
## 
## Dependency note: bot port and lobby port need to be kept in sync with smoke-testing java code
##

set -eux

readonly VERSION=$(sed 's/version\s*=\s*//' game-core/src/main/resources/META-INF/triplea/product.properties)

readonly HTTP_SERVER_JAR_PATH="http-server/build/libs/triplea-http-server-${VERSION}.jar"
readonly BOT_JAR_PATH="game-headless/build/libs/triplea-game-headless-${VERSION}.jar"
readonly SMOKE_TEST_JAR_PATH="smoke-testing/build/libs/triplea-smoke-testing-${VERSION}.jar"

readonly HTTP_SERVER_OUTPUT="server-output"
readonly BOT_OUTPUT="bot-output"


function main() {
  buildJars
  LOBBY_PID=$(startLobby)
  # Give lobby some time to start up
  sleep 5
  BOT_PID=$(startBot)
  
  # On exit, print out the logs for debugging and stop bot and lobby
  trap 'printLogs; kill "$BOT_PID" "$LOBBY_PID"' EXIT

  echo "Waiting for bot to start and connect to lobby..."
  sleep 20
  
  checkAppsStarted
  runJavaClientSmokeTest
  checkLogsForErrors
  echo "SUCCESS! Launched bot and lobby, bot connected to lobby and client was able to connect to both!"
  kill "$LOBBY_PID" "$BOT_PID"
}

function printLogs() {
  echo "-----------"
  echo "PRINTING LOGS"
  echo "-----------"
  echo "Http Server log:"
  cat "$HTTP_SERVER_OUTPUT"
  echo "-----------"
  echo "Bot log:"
  cat "$BOT_OUTPUT"
}


function buildJars() {
  ./gradlew --parallel \
     :http-server:shadowJar \
     :game-headless:shadowJar \
     :smoke-testing:shadowJar 
}

function startLobby() {
  java -jar "$HTTP_SERVER_JAR_PATH" server ./http-server/configuration.yml > "$HTTP_SERVER_OUTPUT" 2>&1 &
  echo $!
}

function startBot() {
  java \
    -Dtriplea.game=Bot01_game \
    -Dtriplea.port=4000 \
    -Dtriplea.name=Bot01Testing \
    -Dtriplea.lobby.uri="http://localhost:8080" \
    -Dtriplea.lobby.game.supportPassword= \
    -Dtriplea.map.folder=~/triplea/downloadedMaps/ \
    -jar "$BOT_JAR_PATH" > "$BOT_OUTPUT" 2>&1 &
  echo $!
}

function checkAppsStarted() {
  nc -z localhost 8080 \
     || (echo "Error!! Lobby is not listening on port 3304! Check logs:" \
            && exit 1)
  nc -z localhost 4000 \
     || (echo "Error!! Bot is not listening on port 4000! Check logs:" \
            && exit 1)
}

function runJavaClientSmokeTest() {
  java -jar "$SMOKE_TEST_JAR_PATH"
}

function checkLogsForErrors() {
  # Do grep looking for 'SEVERE' logging
  # If we find any, echo an error message and exit 1
  # Otherwise, print a confirmation message
  echo "Checking lobby logs for errors..."
  (grep "SEVERE" "$HTTP_SERVER_OUTPUT" \
       && echo "Error!! Found errors in lobby log") \
     && exit 1 \
     || echo "No errors found in lobby logs"
  echo "Checking bot logs for errors..."
  (grep "SEVERE" "$BOT_OUTPUT" \
       && echo "Error!! Found errors in bot log") \
     && exit 1 \
     || echo "No errors found in bot logs"
}

main

